import Dialog from '@vant/weapp/dialog/dialog';
import {
    fetchContactList,
    fetchContactInfo
} from '@/services/contact';
import { getParams } from '@/utils/util'

const app = getApp();
let page_num = 1;
Page({
    data: {
        device_imei: null,
        contacts: [],
        staticPrefix: app.globalData.STATIC_PREFIX
    },
    onLoad(options) {
        const { device_imei } = getParams(options.params)
        this.setData({
            device_imei
        })
    },
    onShow() {
        this.searchList()
     },
    async searchList() {
        let params = {
            page_size: 10,
            page_num,
            device_imei: this.data.device_imei
        }
        const res = await fetchContactList(params);
        if(res.return_code == 'SUCCESS') {
            this.setData({
                contacts: res.data
            })
        }
    },
    async onMakeCall(e) {
        const { id } = e.currentTarget.dataset;
        const res = await fetchContactInfo({ id });
        if(res.return_code == 'SUCCESS') {
            wx.makePhoneCall({
              phoneNumber: res.data,
            })
        }
    },
    onHide() {
        page_num = 1;
        this.setData({
            contacts: []
        })
    }
})